package com.GameSpace.CoreAssets;

import com.GameSpace.SameGame.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

public abstract class Board extends View {
   private static final String logtag = "Board";   
   protected final Rect selRect = new Rect();
   protected int movementCounter;
   protected boolean isStatic;
   protected Grid grid;
   protected Coordinate coordinate;
   
   /* programmer needs to update width and height 
    * via getter and setter*/
   private int width; //board width 
   private int height; //board height 
   
   public Board(Context context) {
      super(context);
      setFocusable(true);
      setFocusableInTouchMode(true);
   }
   
   public void select(int x, int y) {
      coordinate.setXCoord((int)Math.min(Math.max(x, 0), grid.getWidth()));
      coordinate.setYCoord((int)Math.min(Math.max(y, 0), grid.getHeight()));
      getSelectedGridRectangle(coordinate.getXCoord(), coordinate.getYCoord(), selRect);
      invalidate(selRect);
      Log.d(logtag, "select Coord: x,y: " + coordinate.getXCoord() + " " + coordinate.getYCoord());
      Log.d(logtag, "select Grid: x,y: " + grid.getWidth() + " " + grid.getHeight());
   }
   
   public void getSelectedGridRectangle(int x, int y, Rect rect) {
	  float width = grid.getWidth();
	  float height = grid.getHeight();
      rect.set((int) (x * width), (int) (y * height), (int) (x* width + width), (int) (y * height + height));
   }
   
   public void DrawGridLines(Canvas canvas){
	   Paint red = new Paint();
	   red.setColor(getResources().getColor(R.color.red));
	   Paint yellow = new Paint();
	   yellow.setColor(getResources().getColor(R.color.yellow));   
	   float tmpH = grid.getHeight();
	   float tmpW = grid.getWidth(); 
	   //Draw grid lines
	   for (int i = 0; i < BoardSize.getSizeY(); i++) {
	   canvas.drawLine(0, i * tmpH, getBoardWidth(), i * tmpH,red);
	   canvas.drawLine(0, i * tmpH + 1, getBoardWidth(), i * tmpH+ 1, yellow);
	   }
	   for (int i = 0; i < BoardSize.getSizeX(); i++) {
	   canvas.drawLine(i * tmpW, 0, i * tmpW, getBoardHeight(), red);
	   canvas.drawLine(i * tmpW + 1, 0, i * tmpW + 1, getBoardHeight(), yellow);
	   }  	   
   }
   
   public Coordinate getSelectedGridCoordinate(){
	   return coordinate;
   }
   
   public Rect getSelectedRectCoordinate(){
	   return selRect;
   }  
   
   public int getBoardHeight(){
	   return height;
   }
   public int getBoardWidth(){
	   return width;
   }
   
   public void setBoardHeight(int h) {
	   height = h;
   }
   
   public void setBoardWidth(int w) {
	   width = w;
   }  
}

